package com.yunwushanhe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunwushanhe.bean.req.CategoryAddReq;
import com.yunwushanhe.bean.req.CategoryPageListReq;
import com.yunwushanhe.bean.req.CategoryUpdateReq;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.*;
import com.yunwushanhe.constant.Constant;
import com.yunwushanhe.entity.Article;
import com.yunwushanhe.mapper.CategoryMapper;
import com.yunwushanhe.entity.Category;
import com.yunwushanhe.service.ArticleService;
import com.yunwushanhe.service.CategoryService;
import com.yunwushanhe.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-08-05 10:50:03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Override
    public ResponseResult<List<CategoryVO>> getCategoryList() {
        LambdaQueryWrapper<Article> qw = new LambdaQueryWrapper<>();
        qw.eq(Article::getStatus, Constant.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(qw);
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        List<Category> categoryList = listByIds(categoryIds);

        categoryList = categoryList.stream()
                .filter(category -> Constant.CATEGORY_STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());

        return ResponseResult.ok(BeanCopyUtil.copyBeanList(categoryList,CategoryVO.class));
    }

    @Override
    public ResponseResult<List<CategoryAdminVO>> listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,Constant.CATEGORY_STATUS_NORMAL);
        List<Category> categoryList = list(queryWrapper);
        List<CategoryAdminVO> categoryAdminVOS = BeanCopyUtil.copyBeanList(categoryList, CategoryAdminVO.class);
        return ResponseResult.ok(categoryAdminVOS);
    }

    @Override
    public ResponseResult<PageVO<List<CategoryPageListVO>>> getCategoryPageList(CategoryPageListReq categoryPageListReq) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(categoryPageListReq.getName()),
                Category::getName, categoryPageListReq.getName());
        queryWrapper.eq(StringUtils.hasText(categoryPageListReq.getStatus()),
                Category::getStatus,categoryPageListReq.getStatus());

        Page<Category> categoryPage = new Page<>();
        categoryPage.setCurrent(categoryPageListReq.getPageNum());
        categoryPage.setSize(categoryPageListReq.getPageSize());
        page(categoryPage,queryWrapper);

        List<CategoryPageListVO> categoryPageListVOList =
                BeanCopyUtil.copyBeanList(categoryPage.getRecords(), CategoryPageListVO.class);
        PageVO<List<CategoryPageListVO>> pageVO =
                new PageVO<>(categoryPageListVOList, categoryPage.getTotal());
        return ResponseResult.ok(pageVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> addCategory(CategoryAddReq categoryAddReq) {
        Category category = BeanCopyUtil.copyBean(categoryAddReq, Category.class);
        save(category);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<CategoryUpdateVO> getCategoryUpdateById(Long id) {
        Category category = getById(id);
        CategoryUpdateVO categoryUpdateVO =
                BeanCopyUtil.copyBean(category, CategoryUpdateVO.class);
        return ResponseResult.ok(categoryUpdateVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> updateCategoryById(CategoryUpdateReq categoryUpdateReq) {
        HashMap<String, Object> eqMap = new HashMap<>();
        eqMap.put("id",categoryUpdateReq.getId());
        UpdateWrapper<Category> updateWrapper =
                BeanCopyUtil.ignoreNullUpdateWrapper(eqMap, categoryUpdateReq, Category.class);
        update(updateWrapper);
        return ResponseResult.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> deleteCategoryById(String id) {
        String[] arrayId = id.split(",");
        ArrayList<Long> listId = new ArrayList<>();
        for (String s : arrayId) {
            listId.add(Long.valueOf(s));
        }

        removeByIds(listId);
        return ResponseResult.ok();
    }
}

